package leetCode;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * TODO
 *
 * @author 司松坡
 * @createTime 2023年04月06日
 */
public class LeetCode557 {
    public String reverseWords(String s) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        char mid;
        int left = 0, right = 0;
        for (int i = 0; i < length; i++) {
            if (chars[i] == ' ') {
                right = i - 1;
                while (right > left) {
                    mid = chars[right];
                    chars[right] = chars[left];
                    chars[left] = mid;
                    right--;
                    left++;
                }
                left = i + 1;
            }
            if (i == length -1 && chars[i] != ' ') {
                right = i;
                while (right > left) {
                    mid = chars[right];
                    chars[right] = chars[left];
                    chars[left] = mid;
                    right--;
                    left++;
                }
                left = i + 1;
            }
        }
        return  String.valueOf(chars);
    }

    @Test
    private void test(){
        String asbd_dasbgf = reverseWords("asbd dasbgf");
        System.out.println(asbd_dasbgf);
    }
}
